package cn.bfay.lion.rest.service;

import cn.bfay.cache.redis.RedisKeyBuilder;
import cn.bfay.cache.redis.RedisUtils;
import cn.bfay.lion.rest.model.UserSession;
import cn.bfay.lion.rest.redis.RedisKeyTemplate;
import cn.bfay.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * BaseService.
 *
 * @author wangjiannan
 */
@Component
public class BaseService {
    @Autowired
    private HttpServletRequest request;

    protected String getOpenid() {
        return WebUtils.getCookieValue(request, "openid");
    }

    protected UserSession getUserSession() {
        String redisKey = RedisKeyBuilder.generateRedisKey(RedisKeyTemplate.SESSION_TEMPLATE, getOpenid());
        return RedisUtils.getValue(redisKey, UserSession.class);
    }
}
